<?php
namespace ZHMVC\B\TOOL;

class HttpsRequest{
    /**
     * 发送https的get请求
     * @param  string $url
     * @return obj
     */
    public static function get($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output);
    }
    
    public function request_post($url, $data=''){
        $data = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 111 }}}';
        $ch = curl_init();
        $head = array();
        $head[] = 'Content-Type: application/json';
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 300);//
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500);//
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        var_dump($tmpInfo);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }
    
    /**
     * 发送https的POST请求
     * @param  string $url
     * @param  mix    $data json | array
     * @return obj
     */
    public static function post($url, $data = null){
        //if(is_array($data))
           // $data = json_encode($data);
            $data = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 111 }}}';
            
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            if(null != $data){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            $output = curl_exec($curl);
            curl_close($curl);
            return json_decode($output);
    }
}